<template>
  <div class="details">
    <el-card>
      <el-tabs>
        <el-tab-pane label="登录账户设置">
          <el-form label-width="100px">
            <el-form-item label="姓名"
              ><el-input v-model="form.username"></el-input
            ></el-form-item>
            <el-form-item label="密码"
              ><el-input v-model="form.password"></el-input
            ></el-form-item>
            <el-form-item>
              <div style="text-align:center">
                <el-button type="primary" @click="update">更新</el-button>
                <el-button>取消</el-button>
              </div>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情">
          <!-- 个人详情 -->
          <component :is="'UserInfo'" :userInfo="initInfo"></component>
        </el-tab-pane>
        <el-tab-pane label="岗位信息"></el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { sysUser, sysUserPut } from '@/api/user.js'
export default {
  components: { UserInfo: () => import('./components/userInfo.vue') },
  data () {
    return {
      info: '', //个人用户基本信息
      initInfo: {}, // 原始数据
      form: {
        username: '',
        password: ''
      }
    }
  },
  created () {
    this.getUserInfo()
  },
  methods: {
    //   获取员工基本信息
    async getUserInfo () {
      const res = await sysUser(this.$route.params.id)
      res.data.formOfEmployment = res.data.formOfEmployment || 1
      this.info = res.data
      this.initInfo = JSON.parse(JSON.stringify(res.data))
      this.form.username = res.data.username
      console.log('个人信息', res)
    },
    // 更新按钮点击
    async update () {
      const newInfo = { ...this.info, ...this.form }
      await sysUserPut(newInfo)
      this.$message.success('更新成功')
      // 更新完数据之后也要更新原始数据
      this.initInfo = { ...this.initInfo, ...this.form }
      //   this.$router.push('/employees')
    }
  }
}
</script>

<style lang="scss" scoped>
.details {
  padding: 15px;
}
</style>
